import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager


if __name__ == "__main__":
    # 1. 使用 EdgeChromiumDriverManager 自动下载并安装 EdgeDriver
    # edge_service = EdgeService(EdgeChromiumDriverManager().install())
    # edge_service = EdgeService(EdgeChromiumDriverManager().install(), port=8888)  # 换一个端口
    # edge_service = webdriver.Edge()
    # time.sleep(2)

    # 2. 创建 Edge 浏览器驱动对象
    # driver = webdriver.Edge(service=edge_service)
    driver = webdriver.Edge()
    time.sleep(1)
    # 3. 访问百度
    driver.get("https://www.baidu.com/")
    time.sleep(1)

    # 4. 在搜索框输入 "迪丽热巴"
    driver.find_element(By.CSS_SELECTOR, "#chat-textarea").send_keys("劳斯莱斯")
    time.sleep(1)

    # 5. 点击 "百度一下" 按钮
    driver.find_element(By.CSS_SELECTOR, "#chat-submit-button").click()
    time.sleep(1)

    # 6. 关闭浏览器
    driver.quit()

# # 1. 使用 EdgeChromiumDriverManager 自动下载并安装 EdgeDriver
# edge_service = EdgeService(EdgeChromiumDriverManager().install())
#
# # 2. 创建 Edge 浏览器驱动对象
# driver = webdriver.Edge(service=edge_service)
# time.sleep(1)
# # 3. 访问百度
# # driver.get("https://www.baidu.com/")
# driver.get("http://43.136.100.191:8080/")
#
# time.sleep(1)
#
# # ret = driver.find_elements(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li")
# # for i in ret:
# #     print(i.text)
#
# # ret = driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(3) > a > span.title-content-title").click()
# ret = driver.find_element(By.CSS_SELECTOR, "body > div > div.search > input[type=text]").send_keys("string")
#
# title = driver.title
# url = driver.current_url
#
# print(title)
# print(url)
# time.sleep(3)
# # 6. 关闭浏览器
# driver.quit()


# 1. 使用 EdgeChromiumDriverManager 自动下载并安装 EdgeDriver
# edge_service = EdgeService(EdgeChromiumDriverManager().install())

# 2. 创建 Edge 浏览器驱动对象
# driver = webdriver.Edge(service=edge_service)
# time.sleep(1)
# 3. 访问百度
# driver.get("https://www.baidu.com/")

# print("before:"+driver.title)
# print("before:"+driver.current_url)

# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(1)").click()
# time.sleep(2)

# 切换窗口
# curwindow = driver.current_window_handle
# allwindows = driver.window_handles
#
# for window in allwindows:
#     if window != curwindow:
#         driver.switch_to.window(window)
#
#
# print("after:"+driver.title)
# print("after:"+driver.current_url)

# # 设置窗口大小
# # 窗口最大化
# driver.maximize_window()
# time.sleep(1)
# # 窗口最小化
# driver.minimize_window()
# time.sleep(1)
# # 全屏
# driver.fullscreen_window()
# time.sleep(1)
# # 手动设置窗口大小
# driver.set_window_size(1024,764)
# time.sleep(1)
#
# # 屏幕截图
# # 简单版本
# driver.save_screenshot('./image.png')
# # 进阶版本
# filename = "autotest-"+datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')+'.png'
# driver.save_screenshot('./images/'+filename)
#
# # 关闭窗口
# driver.close()

# 弹窗
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(1)").click()
# # 6. 关闭浏览器
# driver.quit()